package com.ruoyi.monitor.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc: 监控设备管理
 * @author：cc
 * @date: 2024/7/23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MonitorService {

    private static final String APP_KEY = "3e81d7b0744f4df3b34370a7a7137212";

    private static final String APP_SECRET = "027c51851d0fcb45ac9104c241593c3e";

    private static final String TOKEN_URL = "https://open.ys7.com/api/lapp/token/get";

    public String getAccessToken() throws Exception {

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(TOKEN_URL);

            List<BasicNameValuePair> urlParameters = new ArrayList<>();
            urlParameters.add(new BasicNameValuePair("appKey", APP_KEY));
            urlParameters.add(new BasicNameValuePair("appSecret", APP_SECRET));
            httpPost.setEntity(new UrlEncodedFormEntity(urlParameters));
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

            try (CloseableHttpResponse response = client.execute(httpPost)) {
                if (response.getCode() != 200) {
                    log.error("调用接口失败, 返回状态码不为200");
                    throw new Exception("调用接口失败");
                }
                String responseJson = EntityUtils.toString(response.getEntity(), "utf-8");
                JSONObject jsonObject = JSON.parseObject(responseJson);
                String status = jsonObject.getString("code");

                if (status.equals("200")) {
                    return jsonObject.getJSONObject("data").getString("accessToken");
                } else {
                    String msg = jsonObject.getString("msg");
                    log.warn("调用获取access token接口失败, 失败信息:{}", msg);
                    throw new Exception(msg);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } catch (IOException e) {
            log.error("出现未知异常", e);
            throw new Exception(e.getMessage());
        }

    }
    public String getToken() throws Exception {
        String loginUrl = "https://cloud.bodazl.com:8090/api/v1/login/login";

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(loginUrl);

            List<BasicNameValuePair> urlParameters = new ArrayList<>();
            urlParameters.add(new BasicNameValuePair("username", "18070033025"));
            urlParameters.add(new BasicNameValuePair("password", "0f883e3949ba59abbe56e057f2e10adc"));
            httpPost.setEntity(new UrlEncodedFormEntity(urlParameters));
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

            try (CloseableHttpResponse response = client.execute(httpPost)) {
                if (response.getCode() != 200) {
                    log.error("调用接口失败, 返回状态码不为200");
                    throw new Exception("调用接口失败");
                }
                String responseJson = EntityUtils.toString(response.getEntity(), "utf-8");
                JSONObject jsonObject = JSON.parseObject(responseJson);
                int code = jsonObject.getIntValue("code");

                if (code == 0) {
                    return jsonObject.getJSONObject("data").getString("token");
                } else {
                    String msg = jsonObject.getString("msg");
                    log.warn("调用登录接口失败, 失败信息:{}", msg);
                    throw new Exception(msg);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } catch (IOException e) {
            log.error("出现未知异常", e);
            throw new Exception(e.getMessage());
        }
    }

}
